package com.ecoolex.ying.space.api.graphql;

import java.util.List;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.NonNull;

import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.util.Assert;
import com.ecoolex.framework.common.util.BeanUtil;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.framework.modularize.api.auth.user.ConsumerMember;
import com.ecoolex.framework.modularize.api.auth.user.ConsumerMemberAuthority;
import com.ecoolex.framework.modularize.api.auth.user.ConsumerMemberManager;
import com.ecoolex.framework.modularize.api.service.storage.FileSize;
import com.ecoolex.framework.modularize.api.service.storage.MimeType;
import com.ecoolex.framework.modularize.api.service.storage.StorageInfo;
import com.ecoolex.framework.modularize.api.service.storage.StorageService;
import com.ecoolex.framework.modularize.api.service.storage.Upload;
import com.ecoolex.framework.modularize.api.service.storage.UploadLimit;
import com.ecoolex.framework.modularize.api.service.storage.UploadSource;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module;
import com.ecoolex.framework.web.api.graphql2.annotation.MutationField;
import com.ecoolex.framework.web.api.graphql2.annotation.QueryField;
import com.ecoolex.framework.web.api.graphql2.annotation.Source;
import com.ecoolex.framework.web.api.graphql2.annotation.TypeField;
import com.ecoolex.ying.space.entity.EstateProjectActivityGoodsOrder;
import com.ecoolex.ying.space.entity.EstateProjectActivityGoodsOrderCustomer;
import com.ecoolex.ying.space.entity.EstateProjectActivityGoodsOrderCustomer.Column;
import com.ecoolex.ying.space.entity.EstateProjectActivityGoodsOrderDetail;
import com.ecoolex.ying.space.enums.EstateProjectActivityGoodsOrderStatus;
import com.ecoolex.ying.space.pojo.AddEstateProjectActivityGoodsOrderCustomerDTO;
import com.ecoolex.ying.space.pojo.EditEstateProjectActivityGoodsOrderCustomerDTO;
import com.ecoolex.ying.space.pojo.SubmitEstateProjectActivityGoodsOrderDTO;
import com.ecoolex.ying.space.ws.EstateProjectActivityGoodsOrderService;
import com.google.common.collect.Lists;

@GraphQL2Module(type = GraphQL2Module.Type.User, description = "地产项目活动商品订单管理")
public class EstateProjectActivityGoodsOrderGraphql {
	
	@Reference
	private EstateProjectActivityGoodsOrderService service;
	
	@Autowired
    private StorageService storageService;
	
	@Autowired
    private ConsumerMemberManager consumerMemberManager;
	
	@TypeField(type = EstateProjectActivityGoodsOrder.class,field = "operationUser",description = "操作用户")
	public ConsumerMember user(@Source EstateProjectActivityGoodsOrder source) {
		return consumerMemberManager.getByUserId(source.getOperationUserId());
	}
	
	@TypeField(type = EstateProjectActivityGoodsOrder.class,field = "canRefundApply",description = "能发起退款申请")
	public boolean canRefundApply(@Source EstateProjectActivityGoodsOrder source) {
		return EstateProjectActivityGoodsOrderStatus.canRefund(source);
	}
	
	@TypeField(type = EstateProjectActivityGoodsOrder.class,field = "detailList",description = "订单明细列表")
	public List<EstateProjectActivityGoodsOrderDetail> detailList(@Source EstateProjectActivityGoodsOrder source){
		return service.detailList(source.getOrderId());
	}
	
	@TypeField(type = EstateProjectActivityGoodsOrder.class,field = "customer",description = "客户信息")
	public EstateProjectActivityGoodsOrderCustomer customer(@Source EstateProjectActivityGoodsOrder source) {
		return service.getCustomerByOrderId(source.getOrderId());
	}
	
	@TypeField(type = EstateProjectActivityGoodsOrderDetail.class,field = "coverImage",description = "活动商品展示图")
	public StorageInfo coverImage(@Source EstateProjectActivityGoodsOrderDetail source) {
		return storageService.get(source.getCoverImage());
	}
	
	@TypeField(type = EstateProjectActivityGoodsOrder.class,field = "paymentImage",description = "付款凭证")
	public StorageInfo coverImage(@Source EstateProjectActivityGoodsOrder source) {
		return storageService.get(source.getPaymentImage());
	}
	
	@QueryField(field = "estateProjectActivityGoodsOrderByOrderNo",description = "活动商品订单详情")
	public EstateProjectActivityGoodsOrder get(@NonNull String orderNo) {
		return service.get(orderNo);
	}
	
	@QueryField(field = "estateProjectActivityGoodsOrder",description = "活动商品订单")
	public EstateProjectActivityGoodsOrder get(@NonNull Integer orderId,ConsumerMemberAuthority user) {
		user.assertLogin();
		return service.getByOperationUserId(orderId, user.userId());
	}
	
	@QueryField(field = "estateProjectActivityGoodsOrderList",description = "活动商品订单列表")
	public Pagination<EstateProjectActivityGoodsOrder> query(Integer status,@NonNull Page page,ConsumerMemberAuthority user){
		user.assertLogin();
		return service.queryByOperationUserId(status, user.userId(), page);
	}
	
	@MutationField(field = "uploadEstateProjectActivityGoodsOrderPaymentImage", description = { "上传地产项目活动商品订单付款凭证" })
    public StorageInfo uploadPaymentImage(@NonNull Upload request, ConsumerMemberAuthority user) {
        user.assertLogin();
		UploadSource uploadSource = new UploadSource("EstateProjectActivityGoodsOrder", "PaymentImage");
        UploadLimit uploadLimit = new UploadLimit();
        uploadLimit.setMaxSize(FileSize.MB.byteLength(10));
        uploadLimit.setAllowMimeType(MimeType.IMAGE.commons());

        return storageService.upload(request, uploadSource, uploadLimit);
    } 

	@MutationField(field = "submitEstateProjectActivityGoodsOrder",description = "提交地产项目活动商品订单")
	public EstateProjectActivityGoodsOrder submit(@NonNull SubmitEstateProjectActivityGoodsOrderDTO submit,ConsumerMemberAuthority user) {
		user.assertLogin();
		submit.setOperationUserId(user.userId());
		return service.submit(submit, user.userName());
	}
	
	@MutationField(field = "refundApplyEstateProjectActivityGoodsOrder",description = "申请地产项目活动商品订单退款")
	public boolean refundApply(@NonNull Integer orderId,ConsumerMemberAuthority user) {
		user.assertLogin();
		EstateProjectActivityGoodsOrder record = service.getByOperationUserId(orderId, user.userId());
		Assert.isTrue(Check.notNull(record),ResultCode.VERIFY.build(1), "活动商品订单不存在");
		service.refundApply(record, user.userName());
		return true;
	}
	
	@MutationField(field = "addEstateProjectActivityGoodsOrderCustomer",description = "添加地产项目活动商品订单客户信息")
	public EstateProjectActivityGoodsOrderCustomer addCustomer(@NonNull AddEstateProjectActivityGoodsOrderCustomerDTO record,ConsumerMemberAuthority user) {
		user.assertLogin();
		EstateProjectActivityGoodsOrder goodsOrder = service.get(record.getOrderId());
		Assert.isTrue(Check.notNull(goodsOrder),ResultCode.VERIFY.build(1),"活动商品订单不存在");
		return service.addCustomer(record, user.userName());
	}
	
	@MutationField(field = "editEstateProjectActivityGoodsOrderCustomer",description = "编辑地产项目活动商品订单客户信息")
	public EstateProjectActivityGoodsOrderCustomer editCustomer(@NonNull EditEstateProjectActivityGoodsOrderCustomerDTO record,ConsumerMemberAuthority user) {
		user.assertLogin();
		EstateProjectActivityGoodsOrderCustomer old = service.getCustomer(record.getId());
		Assert.isTrue(Check.notNull(old),ResultCode.VERIFY.build(1),"活动商品订单客户信息不存在");
		BeanUtil.copy(record,
            old,
            Lists.newArrayList(
            		Column.orderId,
                    Column.deleted,
                    Column.createUser,
                    Column.createTime).stream().map(Column::getJavaProperty).toArray(String[]::new));
		return service.editCustomer(old, user.userName());
	}
}
